﻿using System;
using System.Windows.Forms;
using OrionApplication.OrionLib;
using OrionApplication.OrionLib.Dao;
using OrionApplication.OrionLib.Orion;

namespace OrionApplication.OrionForm
{
    public partial class OrionGroupForm : OrionBaseDoubleForm
    {
        public OrionGroupForm()
        {
            dao = new DaoOrionGroup();
            InitializeComponent();
        }

        public OrionGroupForm(string titre)
            : base(titre)
        {
            dao = new DaoOrionGroup();
            InitializeComponent();
        }

        private void RempliForm(OrionGroup og)
        {
            kTbLibelle.Text = og.Libelle;
            kCbType.Cb.SelectedValue = og.TypeId;
        }

        private OrionGroup RempliObject()
        {
            OrionGroup retour = new OrionGroup();
            retour.Libelle = kTbLibelle.Text;
            retour.TypeId = Convert.ToInt32(kCbType.Cb.SelectedValue);
            return retour;
        }

        private void DupliqueEnregistrement()
        {
            if (kDgv.SelectedRows.Count > 0)
            {
                if (OrionOutils.ChoixOuiNon(OrionConfBase.ConfirmDuplique) == DialogResult.Yes)
                {
                    OrionGroup og = ((DaoOrionGroup)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                    og = ((DaoOrionGroup)dao).Insert(og);
                    this.RempliKDgv();
                    this.FocusSur(og.OrionId);
                }
            }
            else
                OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
        }

        private void SauvegardeForm()
        {
            OrionGroup og = this.RempliObject();
            if (kLblMode.Text == OrionConfBase.OrionModeInsert)
                og = ((DaoOrionGroup)dao).Insert(og);
            else
            {
                if (kDgv.SelectedRows.Count > 0)
                {
                    og.OrionId = Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value);
                    og = ((DaoOrionGroup)dao).Update(og);

                }
                else
                    OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
            }
            this.RempliKDgv();
            this.FocusSur(og.OrionId);
        }

        public void RowEnter()
        {
            if (kDgv.SelectedRows.Count > 0)
            {
                OrionGroup og = ((DaoOrionGroup)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                this.RempliForm(og);
            }
        }
    }
}
